import 'dart:async';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_demo/tmac/firstPage.dart';
import 'package:flutter_demo/tmac/mainMenuPage.dart';
import 'package:flutter_demo/tmac/secondPage.dart';
import 'package:flutter_demo/tmac/util/SystemUtil.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  return runApp(SplashPage());
}

class SplashPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: new SplashStatefulWidget(),
      title: "欢迎页面",
      routes: {
        'firstPage': (BuildContext context) =>FirstPage(),
        'secondPage': (BuildContext context) => MainPage(),
        'mainMenuPage': (BuildContext context) => MainMenuPageStatefulWidget()
      },
    );
  }
}

class SplashStatefulWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new SplashState();
  }
}

class SplashState extends State<SplashStatefulWidget> {
  Timer timer;
  int count = 2;

  @override
  void initState() {
    super.initState();
    LogUtil.init(tag:"flutter_master",isDebug: true,maxLen: 20000);
    SystemUtil.statusLightBar();
    requestPermission();
    timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (count > 0) {
        setState(() {
          count--;
        });
      } else {
        timer.cancel();
        // Navigator.pushReplacementNamed(context, 'mainMenuPage');
        Navigator.pushReplacement(context,
            MaterialPageRoute(builder: (context) {
          return new MainMenuPageStatefulWidget();
        }));
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    if (timer != null) {
      timer.cancel();
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: SafeArea(
        child: Stack(
          children: <Widget>[
            (Align(
                alignment: Alignment.topRight,
                child: Padding(
                  padding: EdgeInsets.only(top: 20, right: 20),
                  child: Container(
                    padding: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                        color: Color(0xffdddddd), shape: BoxShape.circle),
                    child: Text("$count"),
                  ),
                ))),
            Center(
              child: Image.asset(
                'images/icon_splash.png',
                width: 100,
                height: 100,
              ),
            )
          ],
        ),
      ),
    );
  }


  void requestPermission() async {
    //请求权限
    Map<PermissionGroup, PermissionStatus> permissions =
    await PermissionHandler()
        .requestPermissions([PermissionGroup.location,PermissionGroup.camera]);
    //校验权限
    if(permissions[PermissionGroup.camera] != PermissionStatus.granted){
      print("无照相权限");
    }
    if(permissions[PermissionGroup.location] != PermissionStatus.granted){
      print("无定位权限");
    }

  }

}
